<template>
  <div>
    <!-- 菜单信息 用于新增或者修改  应该是一个表单 -->
    <el-dialog
      :title="info.type === 'add' ? '新增' : '修改'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="属性名" prop="specsname">
          <el-input class="input-short" v-model="ruleForm.specsname"></el-input>
        </el-form-item>
        <el-form-item  label="属性值">
            <div class="itm" v-for="(item,index) in attrList" :key="index">
                <el-input class="input-short" v-model="attrList[index]"></el-input>
                <el-button class="del-btn" type="danger" @click="delAttr(index)">删除属性</el-button>
            </div>
          <el-button type="success" @click="addAttr">新增属性</el-button>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addSpecs } from "../../apis";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        // 表单的数据
        specsname: "",
        attrs: "",
        status: 1,
      },
      rules: {
        // 只有标题是必须添的
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
      // 属性值得列表
      attrList: [''],
    };
  },
  methods: {
    // 这个函数的作用就是给ruleForm赋值，以显示表单的内容
    setVal(val) {
        // 字符串转数组 存入attrList
      this.ruleForm = { ...val };
      this.attrList = [...this.ruleForm.attrs]
    },
    // 新增商品属性值
    addAttr(){
        if(this.attrList.length < 5){
            // 最多添加5项
            this.attrList.push('')
        }else{
            this.$message({
              type: "error",
              message: "最多添加五项",
            });
        }
    },
    // 删除商品属性值
    delAttr(index){
        // 通过index确定要删除的数组项
        this.attrList.splice(index,1)
    },
    submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
            // 获取属性值 这个属性值在attrList里
            this.ruleForm.attrs = this.attrList.join()
          let res;
          // 区分新增还是修改
          if (this.info.type === "add") {
            // 新增
            res = await addSpecs(this.ruleForm);
          } else {
            // 修改
            res = await editMenu(this.ruleForm);
          }
          if (res.code == 200) {
            // 更新成功，通知list组件重新获取数据来更新列表
            this.$emit("update");
          }
          // 关闭弹窗
          this.close();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    close() {
      this.info.isShow = false;
    },
  },
};
</script>

<style>
.input-short{
    max-width: 300px;
}
.del-btn{
    margin-left: 10px;
}
.itm{
    margin-bottom: 10px;
}
</style>
